// pages/task/task.js
Page({
  addresses: [],
  selectedFile: null,
  data: {
    type: false, //firmware/material
    fileName: null,
    address: null,
  },
  onHexTap(e) {
    if (this.addresses.length >= 8) return
    const num = e.currentTarget.dataset.content
    if (num) {
      this.addresses.push(e.currentTarget.dataset.content)
    }
    var tempStr = ""
    this.addresses.forEach(hexChar => {
      tempStr = tempStr.concat(hexChar)
    })
    this.setData({
      address: tempStr
    })
  },
  onTypeSwitchChange(res) {
    this.setData({
      type: res.detail.value
    })
  },
  onBackspace(e) {
    if (this.addresses.length == 0) return
    this.addresses.pop()
    var value;
    if (this.addresses.length == 0) {
      value = null
    } else {
      var tempStr = ""
      this.addresses.forEach(hexChar => {
        tempStr = tempStr.concat(hexChar)
      })
      value = tempStr
    }
    this.setData({
      address: value
    })
  },
  onChooseFileTap(e) {
    let that = this
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      extension: ['bin', 'BIN'],
      success(res) {
        that.selectedFile = res.tempFiles[0]
        const tempFilePaths = res.tempFiles[0].name
        that.setData({
          fileName: tempFilePaths
        })
        console.log(res)
        //res.tempFiles[0].path
        //{name: "template_ota.bin",size: 49456,path: "wxfile://tmp_3dbeb0ceed5857ff824a0b8200633c98cc1744d536596693.bin",}
      }
    })
  },
  onSubmit(e) {
      var address = ""
      this.addresses.forEach(hexChar => {
        address = address.concat(hexChar)//合并
      })
      let curPages = getCurrentPages() //获取当前页面js里的所有信息
      let prevPage = curPages[curPages.length - 2] //prevPage 是获取上一个页面的js里面的所有信息 -2 是上一个页面
      prevPage.onFileSelected(this.selectedFile, address)
      wx.navigateBack()
  },
})